/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.view.utils;

import ar.com.gruposicom.controller.AnalizerController;
import ar.com.gruposicom.controller.OperationController;
import ar.com.gruposicom.controller.SystemOut;
import ar.com.gruposicom.domain.Command;
import ar.com.gruposicom.domain.Operation;
import ar.com.gruposicom.domain.Politic;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author marcelo
 */
public class UIPanelUtils 
{
    private Collection<Command> commands = new ArrayList<>();
    private Collection<Politic> politics = new ArrayList<>();
    private Collection<Operation> operations = new ArrayList<>();
    private JTextArea area;
    private Timer timer;

    public UIPanelUtils() {
    }
    
    
    public void init(JTable politics, String minutos, String segundos)
    {
        AnalizerController analizer = new AnalizerController();
        SystemOut salida = new OperationController(this.area, analizer, politics);
        
        timer = analizer.initSecuency(salida, operations, minutos, segundos);
    }
    
    public void load(JTextArea area, JTable politicas)
    {
        this.area = area;
        OperationController controllerOp = new OperationController();
        this.operations = controllerOp.getAll();
        this.loadPolitics(politicas);
    }
    
    private void clearTable(JTable table)
    {
        DefaultTableModel modelo = (DefaultTableModel)table.getModel();
        while(table.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
    }
    
    private void loadPolitics(JTable table)
    {
        this.clearTable(table);
        Iterator<Operation> it = this.operations.iterator();
        System.out.println("Hay "+this.operations.size()+" operaciones");
        while(it.hasNext())
        {
            Operation o = it.next();
            Iterator<Command> c = o.getCommand().iterator();
            while(c.hasNext())
            {
                Command co = c.next();
                System.out.println("Leyendo el comando "+co.getCommand());
                this.commands.add(co);
                Iterator<Politic> p = co.getPolitics().iterator();
                while(p.hasNext())
                {
                    Politic pol = p.next();
                    this.politics.add(pol);
                    System.out.println("Leyendo la politica "+pol.getName());
                    this.viewPolitics(pol, table);
                }
            }
        }
    }
    
    private void viewPolitics(Politic p, JTable table)
    {
        DefaultTableModel model = (DefaultTableModel)table.getModel();
        String data[] = new String[2];
        data[0] = String.valueOf(p.getId());
        data[1] = p.getName();
        model.addRow(data);
    }
    
    public void stop()
    {
        this.timer.stop();
    }
    
    
    
}
